/*
 * Copyright (C) 2011 Project R'yleh Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.Arkham;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Backpack extends ListActivity {
	
    static String[] ITEMS = new String[GameFile.party.inventory.size()];
    static String[] ACTIONS  = {
        "Equip / Unequip" ,
    	"Get Details",
        "Drop"
    };
    int LongClickChoice;
    int WhoPos = 0;
    int equipHere = 0;
    int equipLoc = 0;
    int action = 0;
    String title;
    
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    updateListView();
	    
	    ListView lv = getListView();
	    lv.setTextFilterEnabled(true);
	    
	    lv.setOnItemLongClickListener( new AdapterView.OnItemLongClickListener(){
	    	public boolean onItemLongClick(AdapterView<?> av, View v, int pos, long id) {
	    		
	    	   onLongListItemClick(v,pos,id);
	    	   
	    	   //set to true so that onListItemClick will not activate.
	    	   return true;
	    	}
        }); 

	  }

	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		// Get the item that was clicked
		Object o = this.getListAdapter().getItem(position);
		String keyword = o.toString();
		Toast.makeText(this, keyword, Toast.LENGTH_LONG)
				.show();
		showItem(position);
	}
	
	protected int onLongListItemClick(View v, int pos, long id) {
		LongClickChoice = pos;
		if (GameFile.party.inventory.isEquipped(pos))
			ACTIONS[0] = "Unequip";
		else
			ACTIONS[0] = "Equip";
		new AlertDialog.Builder(this)
		    .setTitle("Do what with " + GameFile.party.inventory.get(pos).getName() + "?")
		    .setItems(ACTIONS,
		    		new DialogInterface.OnClickListener() {
		    	      public void onClick(DialogInterface dialoginterface, int a) {
		    	    	  itemAction(a); //a for Action, as in Action selected.
//		    	    	  action = a;
		    	    	  updateListView();
		    	      }
		    })
		    .show();
		return action;
	}
 
	private void itemAction(int a){
		switch(a){
		case 0:
			// Equip - Unequip
			ArrayList<String> WhereList = new ArrayList<String>();
			ArrayList<Integer> locationIndexesList = new ArrayList<Integer>();
			ArrayList<String> WhoInPartyList = new ArrayList<String>();
			Item What = GameFile.party.inventory.get(LongClickChoice);
			String[] WhoInParty;
			String[] Where;
			int[] locationIndexes;
			
			if (GameFile.party.inventory.isEquipped(LongClickChoice)){
				unequip();
			}
			else{
				//equip
				//Step 1, see where it is equippable
				if(What.isEquipable(Constants.HEAD)){
					WhereList.add("Head");
				    locationIndexesList.add(Constants.HEAD);
				}
				if(What.isEquipable(Constants.BODY)){
					WhereList.add("Body");
				    locationIndexesList.add(Constants.BODY);
				}
				if(What.isEquipable(Constants.ARMS)){
					WhereList.add("Arms");
				    locationIndexesList.add(Constants.ARMS);
				}
				if(What.isEquipable(Constants.R_HAND)){
					WhereList.add("Right Hand");
				    locationIndexesList.add(Constants.R_HAND);
				}
				if(What.isEquipable(Constants.L_HAND)){
					WhereList.add("Left Hand");
				    locationIndexesList.add(Constants.L_HAND);
				}
				if(What.isEquipable(Constants.FEET)){
					WhereList.add("Feet");
				    locationIndexesList.add(Constants.FEET);
				}
				if(What.isEquipable(Constants.ACC)){
					WhereList.add("Accessory");
				    locationIndexesList.add(Constants.ACC);
				}
				
				//step 1b, if nothing selected, it isnt equipable, break
				if(WhereList.isEmpty()){
					Toast.makeText(getApplicationContext(), 
                                   "Item is not equippable", Toast.LENGTH_LONG).show();
					break;
				}
				
				//Step 2, get a list of all characters
				for (int i=0; i<Constants.MAX_CHARACTERS; i++)
					if(GameFile.party.characters[i].getLevel() != 0)
						WhoInPartyList.add(GameFile.party.characters[i].getName());
				
				WhoInParty = new String[WhoInPartyList.size()];
				WhoInPartyList.toArray(WhoInParty);
				locationIndexes = new int[locationIndexesList.size()];
				for(int p=0; p<locationIndexesList.size(); p++)
					locationIndexes[p] = locationIndexesList.get(p);

				Where = new String[WhereList.size()];
				WhereList.toArray(Where);

				Intent i;
				i = new Intent(this, Equiping.class);
				i.putExtra("item", GameFile.party.inventory.get( LongClickChoice));
				i.putExtra("itemStats", GameFile.party.inventory.get(LongClickChoice).getModifiers());
				i.putExtra("players", WhoInParty);
				i.putExtra("locations", Where);
				i.putExtra("locationIndexes", locationIndexes);
				i.putExtra("selection", LongClickChoice);
				startActivity(i);
				
			}
			updateListView();
			break;
		case 1:
			// Get Details
			showItem(LongClickChoice);
			break;
		case 2:
			// Drop
			//Confirm user wishes to drop
			//if yes, then drop
			new AlertDialog.Builder(this)
		    .setTitle("Are you sure you wish to drop " + GameFile.party.inventory.get(LongClickChoice).getName() +
		    		" and loose it forever?")
		    .setItems(R.array.yes_no_array, 
		    		new DialogInterface.OnClickListener() {
		    	      public void onClick(DialogInterface dialoginterface, int a) {
		    	    	  if( a == 0 ){
		    	    		  Toast.makeText(getApplicationContext(), 
	                                   GameFile.party.inventory.get(LongClickChoice).getName() + 
	                                   " was tossed aside in the hallway.", Toast.LENGTH_LONG).show();
		    	    		  if(GameFile.party.inventory.isEquipped(LongClickChoice))
		    	    			  unequip();
		    	    		  GameFile.party.inventory.drop(LongClickChoice);
		    	    	  }
		    	    	  else{
		    	    		  Toast.makeText(getApplicationContext(), 
	                                   GameFile.party.inventory.get(LongClickChoice).getName() + 
	                                   " was placed back into your backpack.", Toast.LENGTH_LONG).show(); 
		    	    	  }
		    	      }
		    })
		    .show();
			updateListView();
			break;
		default:
			updateListView();
		}
		updateListView();
	}
	
	protected void showItem(int position){
		Intent i;
		i = new Intent(this, ItemDetails.class);
		i.putExtra("item", GameFile.party.inventory.get(position));
		i.putExtra("itemStats", GameFile.party.inventory.get(position).getModifiers());
		startActivity(i);
	}
	
	protected void updateListView(){
	    title = "Inventory                                        ";
	    title += "Wallet: $" + GameFile.party.inventory.getMoney();
	    this.setTitle(title);

		ITEMS = new String[GameFile.party.inventory.size()];
	    for( int i=0; i<GameFile.party.inventory.size(); i++){
	    	ITEMS[i] = GameFile.party.inventory.get(i).getName();
	    	if ( GameFile.party.inventory.isEquipped(i) )
	    	   ITEMS[i] += " (E) " + GameFile.party.inventory.get(i).getEquippedBy();
	    }
	    setListAdapter(new ArrayAdapter<String>(this, R.layout.backpack, ITEMS));
	}
    
	protected void unequip(){
		// Equip - Unequip
		String Who = "";
		Item What = GameFile.party.inventory.get(LongClickChoice);
		
		//unequip
		//Step 1, find character who is equipping item.
		Who = What.getEquippedBy();
	    //Step 2, find what position in the party (s)he holds
	    for( int i=0; i<GameFile.party.characters.length; i++)
	    	if( Who == GameFile.party.characters[i].getName() )
	    		WhoPos = i;
	    //step 3, unequip character
		GameFile.party.unequip(GameFile.party.inventory.get(LongClickChoice).getEquippedAt(), 
				               GameFile.party.characters[WhoPos]);
	}
}
